#include <stdio.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include "common.h"
#include "mmap_file.h"
#include "file_op.h"
#include "mmap_file_op.h"
#include "index_handle.h"

using namespace tfs;
using namespace largefile;

const static mode_t fileMode = 0644;

const static uint32_t main_bloc_size = 1024*1024*64;  
const static uint32_t bucket_size = 1000;

static uint32_t block_id = 1;

static struct MMapOption mmap_option = { 1024000 , 4096 ,4096 } ; // 

#if 0

int main(int, char**){

    std::string mainblock_path;
    std::string index_path;
    int32_t  ret = TSF_SUCCESS;

    std::cout<<"pleas input blockid"<<std::endl;
    std::cin>>block_id;

    if( block_id <0 ){
        std::cout<<"input error"<<std::endl;
        return -1;
    }

    // 1. 加载索引文件
    IndexHandle *index_handle = new IndexHandle("." , block_id );

    std::cout<<"load index file.."<<std::endl;
    
    ret = index_handle->load( block_id ,bucket_size,mmap_option );
    if( ret != TSF_SUCCESS ){
        std::cerr<<"in block write test. index load failed."
                 <<" errno:"<<ret
                 <<" block_id:"<<block_id
                 <<" bucket_size:"<<bucket_size<<std::endl;
        index_handle->remove( block_id );
        delete index_handle;
        exit( -2 );
    }

    // 2. 加载主块文件
    std::stringstream tmp_stream;
    tmp_stream <<MAINBLOCK_DIR_PREFIX<<block_id;
    tmp_stream >> mainblock_path;

    FileOperation *fileOP = new FileOperation( mainblock_path ,O_CREAT |O_RDWR | O_LARGEFILE ) ;
    // 3. 将数据写入到主块文件
    char buf[4096] ;
    memset( buf , '6' , sizeof( buf) );
    // 获取未写入数据的起始位置
    int32_t data_offset = index_handle->get_block_data_file_offset();
    // 下一个可分配的文件编号
    int32_t seq_no = index_handle->get_block_info()->seq_no_;

    ret = fileOP->pwrite_file( buf ,sizeof(buf) ,data_offset );
    if( ret  != TSF_SUCCESS ){
        std::cerr<<"in blcok_write_test  main_block pwrite_file error."<<std::endl;
        fileOP->close_file();
        delete index_handle;
        delete fileOP;
        exit(-3);
    }
    // 4.将mateInfo 写入到索引文件
    MetaInfo meta;
    
    meta.set_file_id(seq_no );
    meta.set_offset( data_offset );
    meta.set_size( sizeof(buf) );

    ret  =  index_handle->write_segmen_meta( meta.get_key() ,meta);
    if( ret == TSF_SUCCESS ){
        // 更新块信息 、索引头部信息
        index_handle->commit_blcok_data_file_offset( meta.get_size() ) ;

        index_handle->update_block_info( C_OPER_INSERT , meta.get_size()  );

        // 将内存中的数据，同步到磁盘
        ret = index_handle->flush();
        if( ret != TSF_SUCCESS ){
            std::cerr<<"in block write test  index_handle->flush error."" block id:"<<block_id<<" file seq:"<< seq_no <<std::endl;
        }
    }else{
        std::cerr<<"in block write test index_handle->write_segmen_meta error."<<" block id:"<<block_id<<" file seq:"<< seq_no <<std::endl;
    }

    if( ret != TSF_SUCCESS ){
         std::cerr<<"in block write test write_segmen_meta error."<<" block id:"<<block_id<<" file seq:"<< seq_no <<std::endl;
    }else{
         std::cout<<"in block write test index_handle->write_segmen_meta successfully."<<" block id:"<<block_id<<" file seq:"<< seq_no <<std::endl;
    }

    fileOP->close_file();
    index_handle->flush();

    delete index_handle;
    delete fileOP;

    return 0;
}

#endif